package project05;

/**
 * <p>Title: Project 5</p>
 * <p>Description: CS 235 Project #5
 *       Shows how to get started with displaying card images.</p>
 * @author Anthony Varghese
 * @date   February 5th 2012
 * @version 3.0
 */

public class MainClient extends javax.swing.JFrame {
  /**
   * Data members
   */
  private static final long serialVersionUID = 1L;
  private java.awt.Image image;
  /*
   * Location and name of a file to be displayed
   */
  private static final String directory = "src/project05/CardImages/";
  private static final String file      = "01a.jpg";

  /**
   * Constructor - MainClient
   *    - takes the name of an image file as a parameter 
   * @param fileName
   */
  public MainClient(String fileName) {
    /* 
     * The Toolkit and MediaTracker classes are used to display a JPEG file
     */
	java.awt.Toolkit toolkit = java.awt.Toolkit.getDefaultToolkit();
    image = toolkit.getImage(fileName);
    java.awt.MediaTracker mediaTracker = new java.awt.MediaTracker(this);
    mediaTracker.addImage(image, 0);
    try {
      mediaTracker.waitForID(0);
    } catch (InterruptedException ie) {
      System.err.println(ie);
      System.exit(1);
    }

    // End the program when the window is closed.
    addWindowListener(new java.awt.event.WindowAdapter() {
      /* Anonymous objects again */
      public void windowClosing(java.awt.event.WindowEvent e) {
        System.exit(0);
      }
    });


    int w = image.getWidth(null);

    setSize(w, image.getHeight(null));
    setTitle(fileName);
    setVisible( true );
  }

  /**
   * Paint method - this is where the card image is drawn.
   */
  public void paint(java.awt.Graphics graphics) {
	  super.paint(graphics);
    graphics.drawImage(image, 0, 0, null);
  }

  /**
   * main 
   *    - this is where program execution starts
   * @param args
   */
  public static void main(String[] args) {
    new MainClient(directory + file);
  }
}